import {query,serial,edist,create} from '../services/ShipmentOrder'
import { parse } from 'qs'
import { isEmptyObject } from '../utils'

export default {

  namespace: 'order',

  state: {
    list:[],
    detail:[],
    AddorderTags:false,
    DetailTags:false,
    Editstags:false,
    IndexListStyle:true,
    orderStatus:'',
    pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null,
    },
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/ShipmentOrder') {
          dispatch({
            type: 'query',
            payload: location.query,
          })
          dispatch({
            type: 'showIndex',
          })
        }
      })
    },
  },
  effects: {
    *query ({ payload }, { call, put }) {
        var page = {};  
        let orderStatus ='';
        if(payload.orderStatus && payload.orderStatus !='item_0'){
          orderStatus = payload.orderStatus
        }else{
          orderStatus=''
        }
        if(isEmptyObject(payload.page)){
          page = {total:0,current:1,pageSize:10,field:payload.field,keyword:payload.keyword,orderStatus:orderStatus}
        }else{
          page = {current:payload.page,pageSize:payload.pageSize,field:payload.field,keyword:payload.keyword,orderStatus:orderStatus}
        }
        const data = yield call(query, parse(page))
        if (data) {
            yield put({type:'showDetail'})
            yield put({
              type: 'querySuccess',
              payload: {
                list: data.data.rows,
                pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
                orderStatus:orderStatus,
              }
            })
          }
       
    },
    *showIndex({payload},{call,put}){
      yield put({type:'showIndexList'})
    },
    *detail({payload},{call,put}){
        const data = yield call(serial, payload.orderSerial)
        yield put({type:'showDetail'})
        if(data){
            yield put({
                type: 'detailsit',
                payload: {
                    detail:data.data.rows
                }
            })
        }
    },
    *updates({payload},{call,put}){
        const update = yield call(edist, payload.data)
        if(update){
          const page = {current:payload.page.current,pageSize:payload.page.pageSize}
          const data = yield call(query, parse(page))
          if(data){
            yield put({type:'showDetail'})
            yield put({
              type: 'querySuccess',
              payload: {
                list: data.data.rows,
                pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
              }
            })
          }
        }
    },
    *AddOrder({payload},{call,put}){
      console.log("payload==",payload)
        const Order = yield call(create, payload.data)
        if(Order){
          const page = {current:payload.page.current,pageSize:payload.page.pageSize,orderStatus:payload.orderStatus}
          const data = yield call(query, parse(page))
          if(data){
            yield put({type:'showDetail'})
            yield put({
              type: 'querySuccess',
              payload: {
                list: data.data.rows,
                pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
                orderStatus:payload.orderStatus
              }
            })
          }
        }
    },
    *showEdits({payload},{call,put}){
        const data = yield call(serial, payload.orderSerial)
        if(data){
            yield put({
                type: 'showEdit',
                payload: {
                    detail:data.data.rows
                }
            })
        }
    },
  },

  reducers: {
    querySuccess (state, action) {
      const { list,pagination,orderStatus} = action.payload
      return { ...state,
        list,
        orderStatus,
        pagination: {
          ...state.pagination,
          ...pagination,
        },
       }
    },
    showIndexList(state,action){
      localStorage.setItem("editorder",'true')
      return { ...state, IndexListStyle:true}
    },
    detailsit(state,action){
         const { detail} = action.payload
        return { ...state, detail,IndexListStyle:false}
    },
    showDetail(state,action){
        return { ...state, DetailTags:true,AddorderTags:false,Editstags:false,}
    },
    showAdd(state,action){
        return { ...state, DetailTags:false,AddorderTags:true,Editstags:false}
    },
    showEdit(state,action){
       return { ...state,...action.payload, DetailTags:false,AddorderTags:false,Editstags:true,IndexListStyle:false}
    }
  },

}
